package springtest.dao;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.stereotype.Repository;

import com.mongodb.client.MongoCursor;

import springtest.utils.ClassUtil;

@Repository
public class ImportSolrDao {
	@Autowired
	private SolrTemplate solrTemplate;
	@Autowired
	private MongoTemplate mongoTemplate;
	// 在配置文件中决定solr集合的名字
	@Value("${solr.collection}")
	private String solrCollection;
	// 决定集合的长度
	@Value("${list.size}")
	private int listSize;
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	public void importSolr() {
		List exceptionList = new ArrayList();
		try {
			// 取得包下所有完整类名
			Set<String> className = ClassUtil.getClassName("springtest.pojo", false);
			for (String string : className) {
				// 获得entity所对应的mon中的集合名
				String collectionName = string.split("\\.")[2].toUpperCase();
				// 获得对应的pojo
				Class<?> classtype = Class.forName(string);
				Constructor con = classtype.getDeclaredConstructor();
				// new一个pojo
				Object object2 = con.newInstance();
				// 获得该pojo对应的所有属性
				Field[] declaredFields = classtype.getDeclaredFields();
				// 使用游标去mongodb查询数据
				MongoCursor<Document> cursor = mongoTemplate.getCollection(collectionName).find().iterator();

				List result = new ArrayList();
				// 遍历数据
				while (cursor.hasNext()) {
					Document object = cursor.next();
					exceptionList.add(object.get("_id"));
					for (Field field : declaredFields) {
						// 禁用安全检查
						field.setAccessible(true);
						String name = field.getName();
						Class<?> type = field.getType();
						Object value = object.get(name);
						String typeName = type.getTypeName();
						if (typeName.equals("java.lang.String")) {
							System.out.println(111111);
						}
						if (typeName.equals("java.util.Date")) {
							System.out.println(22222);
						}
						// System.out.println(value);
						if (value != null && value != "") {
							String id = value.toString();
							// 日期类型
							if (name.endsWith("Time")) {
								Date date = (Date) value;
								field.set(object2, date);
							} else {
								// String类型
								field.set(object2, id);
							}
						}
					}
					// 把对象放入集合中
					result.add(object2);
					System.out.println(object2.toString());
					// 如果集合中数据超过一百条就提交保存一次
					if (result.size() > listSize) {
						solrTemplate.saveBeans("new_core", result);
						System.out.println("---------超过限定提交-----------");
						// 提交
						solrTemplate.commit(solrCollection);
						// 清空集合数据
						result.clear();
						exceptionList.clear();
					}
				}
				if (result.size() > 0) {
					// int i = 1 / 0;
					solrTemplate.saveBeans("new_core", result);
					System.out.println("---------未超过限定提交-----------");
					// 提交
					solrTemplate.commit(solrCollection);
					// 清空集合数据
					result.clear();
					exceptionList.clear();

				}
			}
		} catch (Exception e) {
			System.out.println(exceptionList.toString());
			// 打印异常信息
			e.printStackTrace();
		}
	}

	// @Test
	public void testSplit() {
		String s = "com.zeesec.pom.solr.pojo.Ip_item";
		String[] split = s.split("\\.");
		System.out.println(split[5].toUpperCase());
	}

	// @Test
	public void testSplitByUpperCase() {
		String name = "DeviceRawConfig";
		String[] ss = name.split("(?<!^)(?=[A-Z])");
		// for (String string : ss) {
		// System.out.println(string);
		// }
		String name1 = "";
		// if (ss.length == 1) {
		// System.out.println(ss);
		// }

		for (int i = 0; i < ss.length; i++) {
			if (i != ss.length - 1) {
				name1 = name1 + ss[i] + "_";
			} else {
				name1 = name1 + ss[i];
			}
		}
		System.out.println(name1);
	}

	public void testMonNumber() {
		long count = mongoTemplate.getCollection("sso").count();
		System.out.println(count);
	}
}
